package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.exception.UpLoadFileException;
import com.sky.myannotation.FilterTypeUploadFile;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Log4j2
public class CommonController {
    @Autowired
    AliOssUtil aliOssUtil;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    @FilterTypeUploadFile(types = {".png", ".jpg"}) // 限制撒上传类型
    public Result upload(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String upload = aliOssUtil.upload(file.getBytes(), UUID.randomUUID()+originalFilename);
            return Result.success(upload);
        } catch (IOException e) {
            throw new UpLoadFileException(MessageConstant.UPLOAD_FAILED);
        }
    }
}
